package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.CommonDao;
import cn.edu.sdjzu.xg.bysj.dao.DepartmentDao;
import cn.edu.sdjzu.xg.bysj.domain.Department;
import cn.edu.sdjzu.xg.bysj.domain.School;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public final class DepartmentService {
    private static DepartmentDao departmentDao = DepartmentDao.getInstance();
    private static DepartmentService departmentService = new DepartmentService();

    private DepartmentService() {
    }

    public static DepartmentService getInstance() {
        return departmentService;
    }

    public Collection<Department> findAll(List<Condition> conditions_json, Pagination pagination)
            throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Collection<Department> departments = departmentDao.findAll(connection, conditions_json, pagination);
        JdbcHelper.close(connection);

        return departments;
    }

    public Department find(Integer id) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        Department result = departmentDao.find(id, connection);
        JdbcHelper.close(connection);
        return result;
    }

    public boolean update(Department department) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        boolean updated = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            updated = departmentDao.update(department, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为true
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return updated;
    }

    public boolean add(Department department) throws SQLException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        Boolean added = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            added = departmentDao.add(department, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为true
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return added;
    }

    public boolean delete(Integer id) throws SQLException, BysjException {
        //获取数据库连接
        Connection connection = JdbcHelper.getConn();
        Department department = this.find(id);
        boolean deleted = false;
        //将自动提交设置为false
        connection.setAutoCommit(false);
        try {
            deleted = departmentDao.delete(department, connection);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            //回滚
            connection.rollback();
        } finally {
            //将自动提交设置为true
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return deleted;

    }

    public int countAll(School school) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        return departmentDao.countAll(school, connection);
    }

    public boolean delete(Department department_json, Connection connection) throws SQLException, DepartmentService.BysjException {
        boolean result = false;
        int countTeacherOfProfTitle = CommonDao.getInstance().
                countWhereForeignKey(connection, "teacher", "department_id", department_json.getId());
        //若没有二级单位，则能够删除
        if (countTeacherOfProfTitle == 0) {
            result = departmentDao.delete(department_json, connection);
        } else {
            throw new DepartmentService.BysjException(department_json.getDescription() + "有下属系，不能删除");
        }
        JdbcHelper.close(connection);
        return result;
    }

    public class BysjException extends Exception {
        public BysjException(String message) {
            super(message);
        }
    }
}

